/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.ioc.visual.AbstractVisualLoader;

/**
 * Basic visual context loader.
 * 
 * <p>Just display a progression bar.
 * 
 * <p>Directly compile this class using <strong>MTASC</strong>
 * 
 * @author Romain Ecarnot
 */
class fever.ioc.visual.BasicVisualLoader extends AbstractVisualLoader
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _mcBackgound : MovieClip;
	private var _mcBar : MovieClip;	private var _mcBorder : MovieClip;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Default bar width. */
	public static var DEFAULT_BAR_WIDTH : Number = 300;
	
	/** Defaut bar height. */	public static var DEFAULT_BAR_HEIGHT : Number = 10;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * MTASC Access point
	 */
	public static function main( container : MovieClip ) : Void 
	{
		var o  = new BasicVisualLoader( container );
	}
	
	/**
	 * Triggered during loading progression.
	 * 
	 * <p>application-loader node attribute : progress-callback
	 */
	public function onProgress( value : Number ) : Void
	{
		_mcBar._xscale = value; 
	}
	
	/**
	 * Triggered when a loading timeout is broadcast.
	 * 
	 * <p>application-loader node attribute : timeout-callback
	 */
	public function onTimeOut(  ) : Void
	{
		
	}
	
	/**
	 * Triggered when context loading is complete.
	 * 
	 * <p>application-loader node attribute : built-callback
	 */
	public function onBuild(  ) : Void
	{
		_mcBar.removeMovieClip();
		_mcBorder.removeMovieClip();
	}
	
	/**
	 * Triggered when context is parsed and assembled.
	 * 
	 * <p>application-loader node attribute : init-callback
	 */
	public function onInit(  ) : Void
	{
		_mcBackgound.removeMovieClip();
		
		Stage.removeListener( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */ 
	private function BasicVisualLoader( target ) 
	{
		//Target association
		target.__proto__ = this.__proto__;
		target.__constructor__ = BasicVisualLoader;
		this = target;
		
		_mcBackgound = _buildRectangle( 
			this, 
			1, 
			Stage.width, 
			Stage.height, 
			0x000000, 
			0,
			0 
		);
		
		_mcBar = _buildRectangle( 
			this, 
			2, 
			DEFAULT_BAR_WIDTH , 
			DEFAULT_BAR_HEIGHT, 
			0xff0000, 
			75
		);
		_mcBar._xscale = 0;
		
		_mcBorder = _buildRectangle( 
			this, 
			3, 
			DEFAULT_BAR_WIDTH , 
			DEFAULT_BAR_HEIGHT, 
			0, 
			0,
			0xffffff,
			100,
			1 
		);
		
		Stage.addListener( this );
		
		onResize();
	}
	
	private function _buildRectangle( 
		target : MovieClip, 
		d : Number, 
		w : Number, 
		h : Number, 
		fillColor : Number, 
		fillAlpha : Number, 
		borderColor : Number,
		borderAlpha : Number,
		borderThick : Number ) : MovieClip
	{
		var mc : MovieClip = target.createEmptyMovieClip( 
			'__rectangle' + d, 
			d 
		);
		
		mc.beginFill( 
			fillColor ? fillColor : 0x000000, 
			fillAlpha ? fillAlpha : 0 
		);
		mc.lineStyle( 
			borderThick ? borderThick : 1, 
			borderColor ? borderColor : 0x000000, 
			borderAlpha ? borderAlpha : 0 
		);
		
		mc.moveTo( 0, 0 );
		mc.lineTo( w, 0 );
		mc.lineTo( w, h );
		mc.lineTo( 0, h );
		mc.lineTo( 0, 0 );
		mc.endFill();
		
		return mc;
	}
	
	private function onResize(  ) : Void 
	{
		_mcBackgound._width = Stage.width;
		_mcBackgound._height = Stage.height;
		
		_mcBorder._x = Stage.width / 2 - _mcBorder._width /2;		_mcBorder._y = Stage.height / 2 - _mcBorder._height /2;
		
		_mcBar._x = _mcBorder._x;		_mcBar._y = _mcBorder._y;
	}
}